<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace app\api\controller;
use think\Request;
use think\exception\ValidateException;
use app\api\BaseController;
use app\api\validate\Stop as Validate;
use app\api\model\Stop as StopModel;
/**
 * Stop管理
 */
class Stop extends BaseController
{
    /**
     * 显示资源列表
     * 访问：local.stop.com/api/stop/index
     */
    public function index(Request $request)
    {
        if ($this->request->isPost()) {
            try {
                $input = input("post.");
                validate(Validate::class)->scene("index")->check($input);
            } catch ( ValidateException $e ) {
                return json(["status" => "error", "message" => $e->getError()]);
            }
            $count = StopModel::count();
            $page=input("post.page");
            $page_size=input("post.page_size");
            $page=$page?$page:1;
            $page_size=$page_size?$page_size:15;
            $list  = StopModel::page($page, $page_size)->order('id desc')->select();
            foreach ($list as &$datum){
                $imageArr=json_decode($datum['image_arr'],true);
                $datum['image']=$request->domain().$imageArr[0]['url'];
            }
            $totalPages = ceil($count / $page_size);
            $data['list']=$list;
            $data['count']=$count;
            $data['totalPages']=$totalPages;
            return json(["status" => "success", "message" => "请求成功", "data" => $data]);
        }
    }
    
    /**
     * 显示资源详情
     */
    public function detail(Request $request)
    {
        if ($this->request->isPost()) {
            try {
                $input = input("post.");
                validate(Validate::class)->scene("single")->check($input);
            } catch ( ValidateException $e ) {
                return json(["status" => "error", "message" => $e->getError()]);
            }
            $data =(new StopModel())->where('id',$input['id'])->findOrEmpty()->toArray();
            $data['image']=[];
            $imageArr=json_decode($data['image_arr'],true);
            foreach ($imageArr as $item){
                $data['image'][]=$request->domain().$item['url'];
            }
            return json(["status" => "success", "message" => "请求成功", "data" => $data]);
        }
    }
    
    /**
     * 保存新建的资源
     */
    public function save()
    {
        if ($this->request->isPost()) {
            try {
                $input = input("post.");
                validate(Validate::class)->scene("save")->check($input);
            } catch ( ValidateException $e ) {
                return json(["status" => "error", "message" => $e->getError()]);
            }
            StopModel::create($input);
            return json(["status" => "success", "message" => "添加成功"]);
        }
    }
    
    /**
     * 保存更新的资源
     */
    public function update()
    {
        if ($this->request->isPost()) {
            try {
                $input = input("post.");
                validate(Validate::class)->scene("update")->check($input);
            } catch ( ValidateException $e ) {
                return json(["status" => "error", "message" => $e->getError()]);
            }
            StopModel::update($input);
            return json(["status" => "success", "message" => "修改成功"]);
        }
    }
    
    /**
     * 删除指定资源
     */
    public function delete()
    {
        if ($this->request->isPost()) {
            try {
                $input = input("post.");
                validate(Validate::class)->scene("delete")->check($input);
            } catch ( ValidateException $e ) {
                return json(["status" => "error", "message" => $e->getError()]);
            }
            StopModel::destroy($input["ids"]);
            return json(["status" => "success", "message" => "删除成功"]);
        }
    }
    
    /**
     * 自定义更多方法...
     */
}